import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _omit from "lodash/omit";
import _noop from "lodash/noop";
import _map from "lodash/map";
import _isFunction from "lodash/isFunction";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class;

import React from 'react'; // import PropTypes from 'prop-types';

import { Button, Modal, Spin } from 'antd';
import { Bind } from 'lodash-decorators'; // import { queryConfigsByPageCode } from 'cpaas-front-cpfm/lib/services/uiPageService';

import { getCurrentOrganizationId, getResponse, isPromise } from "../../../utils/utils";
import request from "../../../utils/request";
import { dealObjectProps, getContextValue, get } from "../utils";
import { dynamicModalOmitProps, modalBtnSep } from "../config";

function queryConfigsByPageCode(organizationId, pageCode) {
  return request("cpfm/v1/".concat(organizationId, "/ui-pages/common/").concat(pageCode), {
    method: 'GET'
  });
}

var DynamicModal = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicModal, _React$Component);

  var _super = _createSuper(DynamicModal);

  function DynamicModal(props) {
    var _this;

    _classCallCheck(this, DynamicModal);

    _this = _super.call(this, props);
    _this.ref = {};
    _this.state = {
      visible: false,
      loading: false // modalBtnProps: {}, // {footer: []} 按钮组

    };
    return _this;
  }

  _createClass(DynamicModal, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          _this$props$type = _this$props.type,
          type = _this$props$type === void 0 ? 'drawer' : _this$props$type,
          _this$props$destroyOn = _this$props.destroyOnClose,
          destroyOnClose = _this$props$destroyOn === void 0 ? true : _this$props$destroyOn;
      var _this$state = this.state,
          _this$state$pageModal = _this$state.pageModalProps,
          pageModalProps = _this$state$pageModal === void 0 ? {} : _this$state$pageModal,
          _this$state$modalBtnP = _this$state.modalBtnProps,
          modalBtnProps = _this$state$modalBtnP === void 0 ? {} : _this$state$modalBtnP;
      var typeProps = type === 'modal' ? modalProps : drawerProps;
      var dealProps = dealObjectProps(_omit(this.props, dynamicModalOmitProps), this);
      this.dealProps = dealProps;
      var _this$state2 = this.state,
          visible = _this$state2.visible,
          loading = _this$state2.loading;
      return /*#__PURE__*/React.createElement(Modal, Object.assign({}, pageModalProps, typeProps, dealProps, modalBtnProps, {
        visible: visible,
        onOk: this.handleOkBtnClick,
        onCancel: this.handleCancelBtnClick,
        destroyOnClose: destroyOnClose
      }), /*#__PURE__*/React.createElement(Spin, {
        spinning: loading
      }, this.renderDynamicComponents()));
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;
      var pageCode = this.state.pageCode;

      if (_isFunction(onRef)) {
        onRef(this);
      }

      if (pageCode) {
        this.updateModalPage();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      // 根据 pageCode 更新 fields
      var pageCode = this.state.pageCode;

      if (pageCode && pageCode !== prevState.pageCode) {
        this.updateModalPage();
      }

      if (this.props.modalBtns && this.props.modalBtns !== prevProps.modalBtns) {
        this.updateModalBtns();
      }
    }
  }, {
    key: "updateModalPage",
    value: function updateModalPage() {
      var _this2 = this;

      var pageCode = this.state.pageCode;
      var _this$props$organizat = this.props.organizationId,
          organizationId = _this$props$organizat === void 0 ? getCurrentOrganizationId() : _this$props$organizat;
      queryConfigsByPageCode(organizationId, pageCode).then(function (res) {
        var pageConfig = getResponse(res) || {};

        if (pageConfig.script) {
          eval(pageConfig.script)(_this2); // eslint-disable-line
        }

        _this2.setState({
          fields: pageConfig.fields,
          pageModalProps: {
            title: pageConfig.description // 页面描述

          }
        });
      }).then(function () {
        _this2.setState({
          loading: false
        });
      });
    }
  }, {
    key: "updateModalBtns",
    value: function updateModalBtns() {
      var modalBtns = this.props.modalBtns;
      var that = this;

      var footerBtns = _map(modalBtns, function (modalBtn, index) {
        var _split = (modalBtn || '').split(modalBtnSep),
            _split2 = _slicedToArray(_split, 3),
            keyAndDescription = _split2[0],
            type = _split2[1],
            contextPath = _split2[2];

        var key = keyAndDescription || index;

        function modalBtnClick() {
          return _modalBtnClick.apply(this, arguments);
        }

        function _modalBtnClick() {
          _modalBtnClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var func;
            return _regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    func = getContextValue(that, contextPath);

                    if (!_isFunction(func)) {
                      _context.next = 12;
                      break;
                    }

                    _context.prev = 2;
                    _context.next = 5;
                    return func();

                  case 5:
                    that.hide();
                    _context.next = 10;
                    break;

                  case 8:
                    _context.prev = 8;
                    _context.t0 = _context["catch"](2);

                  case 10:
                    _context.next = 13;
                    break;

                  case 12:
                    // 没有绑定到方法就直接关闭
                    that.hide();

                  case 13:
                  case "end":
                    return _context.stop();
                }
              }
            }, _callee, null, [[2, 8]]);
          }));
          return _modalBtnClick.apply(this, arguments);
        }

        return /*#__PURE__*/React.createElement(Button, {
          key: key,
          type: type,
          onClick: modalBtnClick
        }, keyAndDescription);
      });

      this.setState({
        modalBtnProps: {
          footer: footerBtns
        }
      });
    }
  }, {
    key: "renderDynamicComponents",
    value: function renderDynamicComponents() {
      var _this3 = this;

      var _this$state3 = this.state,
          _this$state3$params = _this$state3.params,
          params = _this$state3$params === void 0 ? {} : _this$state3$params,
          _this$state3$fields = _this$state3.fields,
          fields = _this$state3$fields === void 0 ? [] : _this$state3$fields;
      var DynamicComponent = get('DynamicComponent');
      return _map(fields, function (field) {
        return /*#__PURE__*/React.createElement(DynamicComponent, {
          key: field.templateCode,
          context: _this3,
          onRef: _this3.handleRef(field.templateCode),
          template: field,
          params: params
        });
      });
    }
  }, {
    key: "handleRef",
    value: function handleRef(dynamicRef) {
      var _this4 = this;

      return function (dynamicComponent) {
        _this4.ref[dynamicRef] = dynamicComponent;
      };
    }
  }, {
    key: "show",
    value: function show() {
      var _this5 = this;

      var _this$dealProps = this.dealProps,
          beforeShow = _this$dealProps.beforeShow,
          afterShow = _this$dealProps.afterShow;
      var afterShowCallback = afterShow;

      if (!_isFunction(afterShow)) {
        afterShowCallback = _noop;
      }

      if (_isFunction(beforeShow)) {
        var beforeShowReturn = beforeShow();

        if (beforeShowReturn === false) {
          return false;
        }

        if (isPromise(beforeShowReturn)) {
          beforeShowReturn.then(function () {
            _this5.setState({
              visible: true
            });

            afterShowCallback();
          }, function () {});
        } else {
          this.setState({
            visible: true
          });
          afterShowCallback();
        }
      } else {
        this.setState({
          visible: true
        });
        afterShowCallback();
      }
    }
  }, {
    key: "hide",
    value: function hide() {
      var _this6 = this;

      var _this$dealProps2 = this.dealProps,
          beforeHide = _this$dealProps2.beforeHide,
          afterHide = _this$dealProps2.afterHide;
      var afterHideCallback = afterHide;

      if (!_isFunction(afterHide)) {
        afterHideCallback = _noop;
      }

      if (_isFunction(beforeHide)) {
        var beforeHideReturn = beforeHide();

        if (beforeHideReturn === false) {
          return false;
        }

        if (isPromise(beforeHideReturn)) {
          beforeHideReturn.then(function () {
            _this6.setState({
              visible: false
            });

            afterHideCallback();
          });
        } else {
          this.setState({
            visible: false
          });
          afterHideCallback();
        }
      } else {
        this.setState({
          visible: false
        });
        afterHideCallback();
      }
    }
  }, {
    key: "handleOkBtnClick",
    value: function handleOkBtnClick() {
      var _this7 = this;

      var _this$dealProps3 = this.dealProps,
          onOk = _this$dealProps3.onOk,
          afterOk = _this$dealProps3.afterOk;
      var afterOkCallback = afterOk;

      if (!_isFunction(afterOk)) {
        afterOkCallback = _noop;
      }

      if (_isFunction(onOk)) {
        var onOkReturn = onOk();

        if (onOkReturn === false) {
          return false;
        }

        if (isPromise(onOkReturn)) {
          onOkReturn.then(function () {
            _this7.setState({
              visible: false
            });

            afterOkCallback();
          }, function () {
            _this7.setState({
              visible: true
            });
          });
        } else {
          this.setState({
            visible: false
          });
          afterOkCallback();
        }
      } else {
        this.setState({
          visible: false
        });
        afterOkCallback();
      }
    }
  }, {
    key: "handleCancelBtnClick",
    value: function handleCancelBtnClick() {
      this.hide();
    }
  }, {
    key: "init",
    value: function init(params) {
      this.setState({
        params: params
      });
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      var nextState = {};

      if (nextProps.params && nextProps.params !== prevState.params) {
        nextState.params = nextProps.params;
      }

      if (nextProps.fields && nextProps.fields !== prevState.fields) {
        nextState.fields = nextProps.fields;
      }

      if (nextProps.pageCode && nextProps.pageCode !== prevState.pageCode) {
        nextState.pageCode = nextProps.pageCode;
        nextState.loading = true;
      }

      if (_isEmpty(nextState)) {
        return null;
      }

      return nextState;
    }
  }]);

  return DynamicModal;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "updateModalPage", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "updateModalPage"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "updateModalBtns", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "updateModalBtns"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRef", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleRef"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "show", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "show"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "hide", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "hide"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleOkBtnClick", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "handleOkBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleCancelBtnClick", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "handleCancelBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "init", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "init"), _class.prototype)), _class));
export { DynamicModal as default };
var modalProps = {};
var drawerProps = {
  wrapClassName: 'ant-modal-sidebar-right',
  transitionName: 'move-right'
}; // 暴露出去的方法

DynamicModal.exportFuncs = [];
DynamicModal.exportFuncsInfo = {};